<script lang="ts" setup>
import { ref } from 'vue';
import type {Car, CarListParams, Rule} from '@/types/card'
import { getRuleListAPI } from '@/apis/card';
import { utils, writeFileXLSX } from 'xlsx';
import useUtils from 'element-plus/es/components/table/src/table/utils-helper.mjs';

const ruleList = ref<Car[]>([])
const params = ref<CarListParams>({
  page: 1,
  pageSize: 10
})
const total = ref(0)
const dialogVisible = ref(false)

//获取规则列表
const getRuleList = async () => {
  const res = await getRuleListAPI(params.value)
  console.log('res', res);
  ruleList.value = res.data.rows
  total.value = res.data.total
}
getRuleList()

// const chargeType: any = {
//   'duration': '时长收费',
//   'turn': '按次收费',
//   'partition': '分段消费'
// }

// 计费规则
const chargeTypeMap = new Map<Rule['chargeType'], string>([
  // [key, value] 键值对
  ['duration', '时长收费'],
  ['turn', '按次收费'],
  ['partition', '分段消费']
]);

// const exportToExcel = () => {
//   //1.创建工作簿(xlsx文件)
//   const workbook = utils.book_new()
//   //2.创建工作格(表格)
//   const wordsheet = utils.json_to_sheet([
//     { name: '张三', age: 18 },
//     { name: '李四', age: 20 }
//   ])
//   //3.将表格添加到工作簿里
//   utils.book_append_sheet(workbook, wordsheet, 'Data')
//   utils.sheet_add_aoa(wordsheet, [['姓名', '年龄']], {origin: 'D5'})
//   //4.导出下载文件
//   writeFileXLSX(workbook,'测试用.xlsx')
// }

// const exportToExcel = () => {
//   //1.创建工作簿(xlsx文件)
//   const workbook = utils.book_new()
//   //2.创建工作格(表格)
//   const worksheet = utils.json_to_sheet(ruleList.value.map((item, index) => ({
//     ...item,
//     id: index + 1, //把id覆盖成序号
//     chargeType: chargeTypeMap.get(item.chargeType) //把chargeType覆盖成中文
//   })))
//   //3.将表格添加到工作簿里
//   utils.book_append_sheet(workbook, worksheet, '计费规则')
//   //4.覆盖默认标题(表格，二维数组，标题位置)
//   utils.sheet_add_aoa(
//     worksheet,
//     [
//       ['序号', '计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上限(元)', '计费方式', '计费规则'],
//     ],
//     { origin: 'A1' }
//   )
//     //5.导出下载xlsx文件
//   writeFileXLSX(workbook, '停车计费规则.xlsx')
// }
</script>

<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>
      <!-- <el-button @click="exportToExcel()">导出Excel</el-button> -->
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="{row}">
            <!-- {{ chargeType[scope.row.chargeType] }} -->
               {{ chargeTypeMap.get(row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template>
            <el-button size="small" type="text">编辑</el-button>
            <el-button size="small" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding: 0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>